package com.run.web.backend;

import com.github.pagehelper.Page;
import com.run.model.EbProduct;
import com.run.model.EbProductCategory;
//import com.run.service.ProductCategoryService;
import com.run.service.ProductClassService;
import com.run.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/product")//工号
public class ProductController {

    //后台
    //1.商品的分页查询方法——分页查询的请求
//    @RequestMapping("/list")
//    public String list(){
//        System.out.println("请求成功!");
//        return  null;
//    }
    @Autowired
    private ProductService productService;//低耦合

    @Autowired
    private ProductClassService productClassService;

    //1.商品的分页查询方法---分页查询的请求
    //分页：${pageContext.request.contextPath}/product/list?page=当前页-1
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page",defaultValue = "1") int page,
                       @RequestParam(value = "pageSize",defaultValue = "5")  int pageSize){
        System.out.println("receive browser proruct page request!!!");
        //1.调用service层的分页查询商品信息的方法
        List<EbProduct> ebProducts = productService.list(page, pageSize);
        //2.返回model(ebProducts)和视图(jsp)
        model.addAttribute("ebProducts",ebProducts);//key-value  你名字---你

        //2.1.传递当前页到product.jsp
        model.addAttribute("curPage",page);
        //2.2 传递最大页到product.jsp
        model.addAttribute("maxPage",((Page)ebProducts).getPages());

        return "manage/product";
    }
    //2.商品添加的方法---商品添加的请求
    @RequestMapping("/to_save")
    public String toSave(Model model){
        //查询所有大类和小类
        //1.调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap = productClassService.findBigAndSmallCategory();
        //2.通过model传递给jsp
        model.addAttribute("categoryMap",categoryMap);

        return "manage/product-add";
    }
    @RequestMapping("/save")
    public String save(EbProduct ebProduct,
                       MultipartFile uploadFile,
                       HttpServletRequest request){
        System.out.println("ebProduct==>"+ebProduct);
        System.out.println(uploadFile);
        if(uploadFile!=null) {
            //0.1 获取上传文件的文件名  1.jpg
            String originalFileName = uploadFile.getOriginalFilename();
            //0.2 生成一个新的文件名  xxxx  + .jpg
            String newFileName = UUID.randomUUID().toString() + originalFileName.substring(originalFileName.indexOf("."));
            //0.3 构建file对象
            //1)获取服务器的images/product 路径ß
            String rootPath = request.getServletContext().getRealPath("/") + "images/product";

            File file = new File(rootPath, newFileName);
            //0.4 将文件上传到服务器images/product
            try {
                uploadFile.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //1.调用商品的service层的添加商品的方法
            //假设所有的商品图片都是1.jpg
            //ebProduct.setEpFileName("1.jpg");
            //1.2 设置上传的文件真实名字
            ebProduct.setEpFileName(newFileName);


            int result = productService.save(ebProduct);
            //2.根据添加的结果(result)跳转页面
            if(result<=0){
                return "manage/manage-error";
            }
            return "manage/manage-result";
        }else{
            return "manage/manage-error";
        }
    }
    //3.删除商品的方法---删除商品的请求

    //4.更新商品的方法---更新商品的请求
    @RequestMapping("/to_edit")
    public String toEdit(Model model,Integer epId){
        System.out.println("epId==>"+epId);
        //1.调用service的根据商品id查询该商品所有信息
        EbProduct ebProduct = productService.selectOne(epId);
        //2. model（数据）和jsp（视图）

        //2.1 绑定显示的商品对象
        model.addAttribute("ebProduct",ebProduct);
        //2.2.查询所有大类和小类
        //1.调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap = productClassService.findBigAndSmallCategory();
        //2.通过model传递给jsp
        model.addAttribute("categoryMap",categoryMap);


        return "manage/product-modify";
    }

    @RequestMapping("/edit")
    public String edit(EbProduct ebProduct,
                       MultipartFile uploadFile,
                       HttpServletRequest request){
        System.out.println(ebProduct);
        System.out.println(uploadFile);
        if(uploadFile.getSize()>0){
            //0.1 获取上传文件的文件名  1.jpg
            String originalFileName = uploadFile.getOriginalFilename();
            //0.2 生成一个新的文件名  xxxx  + .jpg
            String newFileName = UUID.randomUUID().toString() + originalFileName.substring(originalFileName.indexOf("."));
            //0.3 构建file对象
            //1)获取服务器的images/product 路径ß
            String rootPath = request.getServletContext().getRealPath("/") + "images/product";

            File file = new File(rootPath, newFileName);
            //0.4 将文件上传到服务器images/product
            try {
                uploadFile.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //设置修改的文件的名称
            ebProduct.setEpFileName(newFileName);

        }
        //1.调用service更新商品的方法
        int result = productService.update(ebProduct);
        //2.根据添加的结果(result)跳转页面
        if(result<=0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }


    //前台
    @RequestMapping("/detail")
    public String detail(Model model,Integer epId){
        //1.调用service层根据商品编号查询商品详情的方法
        EbProduct ebProduct=productService.selectOne(epId);
        //2.绑定数据到model
        model.addAttribute("ebProduct",ebProduct);
        return "product-view";
    }

}
